package com.oscar.practice.shiro.action.user;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.oscar.practice.shiro.biz.user.UserBiz;
import com.oscar.practice.shiro.dao.user.UserDao;
import com.oscar.practice.shiro.model.user.UserInfo;
import com.oscar.practice.shiro.util.lang.JsonUtil;
import com.oscar.practice.shiro.util.lang.PageUtils;

/**
 * 用户管理
 *
 * @author tongyufu
 *
 */
@RestController
@RequestMapping("/admin/user")
public class UserAction {

    @Autowired
    private UserBiz userBiz;
    @Autowired
    private UserDao userDao;

    @RequestMapping("search")
    public String search(@RequestParam Map<String, Object> params) {
        PageUtils.processPage(params);
        return JsonUtil.toPageJson(userDao.search(params), userDao.searchTotal(params));
    }

    @RequestMapping("queryById")
    public UserInfo queryById(Integer id) {
        return userDao.queryById(id);
    }

    @RequestMapping("save")
    //@RequiresPermissions(value = { "user:add", "user:update" }, logical = Logical.OR)
    public String saveUser(UserInfo usersInfo, Integer[] roles) {
        return userBiz.save(usersInfo, roles);
    }

    @RequestMapping("delete")
    public String delete(Integer id) {
        userBiz.delete(id);
        return JsonUtil.toFormJson("操作成功", true);
    }

    @RequestMapping(value = "password", method = RequestMethod.POST)
    public String updatePassword(@RequestParam Map<String, String> params) throws Exception {
        return userBiz.updatePassword(params);
    }

}
